using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using dal = PConsulting.DataAccess.TimeRegistration;
using PConsulting.DataSets;

namespace PConsulting.Business
{
    public class Registration
    {
        #region Properties
        private int registrationId;

        public int RegistrationId
        {
            get { return registrationId; }
            set { registrationId = value; }
        }
        private int projectId;

        public int ProjectId
        {
            get { return projectId; }
            set { projectId = value; }
        }
        private DateTime registrationDate;

        public DateTime RegistrationDate
        {
            get { return registrationDate; }
            set { registrationDate = value; }
        }
        private double? hours100;

        public double? Hours100
        {
            get { return hours100; }
            set { hours100 = value; }
        }
        private double? hours150;

        public double? Hours150
        {
            get { return hours150; }
            set { hours150 = value; }
        }
        private double? hours200;

        public double? Hours200
        {
            get { return hours200; }
            set { hours200 = value; }
        }
        private DateTime? startHour;

        public DateTime? StartHour
        {
            get { return startHour; }
            set { startHour = value; }
        }
        private DateTime? endHour;

        public DateTime? EndHour
        {
            get { return endHour; }
            set { endHour = value; }
        }
        private string remarks;

        public string Remarks
        {
            get { return remarks; }
            set { remarks = value; }
        }
        private double? standByCompensation;

        public double? StandByCompensation
        {
            get { return standByCompensation; }
            set { standByCompensation = value; }
        }
        private double? shiftCompensation;

        public double? ShiftCompensation
        {
            get { return shiftCompensation; }
            set { shiftCompensation = value; }
        }
        private double? km;

        public double? Km
        {
            get { return km; }
            set { km = value; }
        }
        private double? absenceHours;

        public double? AbsenceHours
        {
            get { return absenceHours; }
            set { absenceHours = value; }
        }
        #endregion

        public List<Registration> SelectByProject(int projectId)
        {
            dal.Registration reg = new dal.Registration();
            DataSet ds = reg.SelectByProject(projectId);
            List<Registration> registrations = new List<Registration>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                registrations.Add(FillObject(row));
            }
            return registrations;
        }

        public List<Registration> SelectByProjectMonth(int projectId, DateTime month)
        {
            dal.Registration reg = new dal.Registration();
            DataSet ds = reg.SelectByProjectMonth(projectId, month);
            List<Registration> registrations = new List<Registration>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                registrations.Add(FillObject(row));
            }
            return registrations;
        }

        public List<Registration> SelectByUsernameMonth(string userName, DateTime month)
        {
            dal.Registration reg = new dal.Registration();
            DataSet ds = reg.SelectByUsernameMonth(userName, month);
            List<Registration> registrations = new List<Registration>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                registrations.Add(FillObject(row));
            }
            return registrations;
        }

        public void Update(List<Registration> registrations, int projectId, DateTime registrationDate)
        {
            //delete all registrations, and re-insert them
            // ==> existing registrations with no current values will be cleaned-up
            dal.Registration dalReg = new dal.Registration();

            dalReg.Delete(projectId, new DateTime(registrationDate.Year, registrationDate.Month, 1), new DateTime(registrationDate.AddMonths(1).Year, registrationDate.AddMonths(1).Month, 1));

            foreach (Registration reg in registrations)
            {
                dalReg.Insert(reg.projectId, reg.registrationDate, reg.hours100, reg.hours150, reg.hours200, reg.startHour, reg.endHour, reg.remarks, reg.standByCompensation, reg.shiftCompensation, reg.km);
            }
        }

        private Registration FillObject(DataRow row)
        {
            Registration reg = new Registration();
            reg.RegistrationId = (int)row["registrationId"];
            reg.projectId = (int)row["projectId"];
            reg.registrationDate = (DateTime)row["registrationDate"];
            if (row.IsNull("hours100"))
                reg.hours100 = null;
            else
                reg.hours100 = (double)row["hours100"];
            if (row.IsNull("hours150"))
                reg.hours150 = null;
            else
                reg.hours150 = (double)row["hours150"];
            if (row.IsNull("hours200"))
                reg.Hours200 = null;
            else
                reg.hours200 = (double)row["hours200"];
            if (row.IsNull("startHour"))
                reg.startHour = null;
            else
                reg.startHour = (DateTime)row["startHour"];
            if (row.IsNull("endHour"))
                reg.endHour = null;
            else
                reg.endHour = (DateTime?)row["endHour"];
            if (row.IsNull("remarks"))
                reg.remarks = "";
            else
                reg.remarks = (string)row["remarks"];
            if (row.IsNull("standbyCompensation"))
                reg.standByCompensation = null;
            else
                reg.standByCompensation = (double)row["standbyCompensation"];
            if (row.IsNull("shiftCompensation"))
                reg.shiftCompensation = null;
            else
                reg.shiftCompensation = (double)row["shiftCompensation"];
            if (row.IsNull("km"))
                reg.km = null;
            else
                reg.km = (double)row["km"];

            return reg;
        }

        public tdsTimesheetOverview SelectOverviewByMonth(DateTime month)
        {
            dal.Registration reg = new dal.Registration();
            return reg.SelectOverviewByMonth(month);
        }

        public tdsTimesheets SelectTimesheet(int projectId, DateTime month)
        {
            dal.Registration reg = new dal.Registration();
            return reg.SelectTimesheet(projectId, month);
        }

        #region Update Status

        public void SetVerified(int projectId, DateTime month)
        {
            dal.Registration reg = new dal.Registration();
            reg.UpdateTimesheet(projectId, month, TimesheetStatus.Verified.ToString());
        }
        public void SetInvalid(int projectId, DateTime month)
        {
            dal.Registration reg = new dal.Registration();
            reg.UpdateTimesheet(projectId, month, TimesheetStatus.Invalid.ToString());
        }
        public void SetInvoiced(int projectId, DateTime month)
        {
            dal.Registration reg = new dal.Registration();
            reg.UpdateTimesheet(projectId, month, TimesheetStatus.Invoiced.ToString());
        }

        #endregion
    }

}
